package com.rxjava;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cjt2325.cameraview.R;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

public class RXJavaActivity extends AppCompatActivity {

    private TextView id_content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_rxjava);

        id_content = (TextView) findViewById(R.id.id_content);
    }

    public void RXJavaClick(View view){
        switch (view.getId()){
            case R.id.button1:
                Observable.create(new ObservableOnSubscribe<String>() {
                    @Override
                    public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                        Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                null, null, null, null);
                        while (cursor.moveToNext()){
                            byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                            String path = new String(data, 0, data.length - 1);
                            emitter.onNext(path);
                        }
                        cursor.close();
                    }
                }).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                Log.i("111", "onSubscribe");
                            }

                            @Override
                            public void onNext(String s) {
                                Log.i("111", "onNext========\t\t\t" + s);
                                id_content.append(s + "\n");
                            }

                            @Override
                            public void onError(Throwable e) {
                                Log.i("111", "onError");
                            }

                            @Override
                            public void onComplete() {
                                Log.i("111", "onComplete");
                            }
                        });
                break;
            case R.id.button2:
                String folder = Environment.getExternalStorageDirectory().getAbsolutePath();
                File dirFile = new File(folder);
                File[] folders = new File[]{dirFile};

                Observable.fromArray(folders)
                        .flatMap(new Function<File, ObservableSource<File>>() {
                            @Override
                            public ObservableSource<File> apply(File file) throws Exception {
                                return Observable.fromArray(file.listFiles());
                            }

                        })
                        .filter(new Predicate<File>() {//添加筛选器
                            @Override
                            public boolean test(File file) throws Exception {//对每个事件进行筛选，返回true的保留
                                return file.getName().endsWith(".png");
                            }
                        })
                        .map(new Function<File, String>() {

                            @Override
                            public String apply(File file) throws Exception {
                                return file.getAbsolutePath();
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<String>() {
                            @Override
                            public void accept(String o) throws Exception {
                                Log.i("111", o);
                            }

                        });


                break;
        }
    }
}
